package com.goodomen.garbage.web.web.controller.back.system;

import com.github.pagehelper.PageInfo;
import com.goodomen.garbage.common.persistence.model.system.SysAdmin;
import com.goodomen.garbage.web.constant.Constant;
import com.goodomen.garbage.web.constant.ResponseCodeEnum;
import com.goodomen.garbage.web.dto.SysAdminDto;
import com.goodomen.garbage.web.dto.response.PageResponseDto;
import com.goodomen.garbage.web.dto.response.ResponseDto;
import com.goodomen.garbage.web.service.system.SysAdminService;
import com.goodomen.garbage.web.util.AESUtil;
import com.goodomen.garbage.web.web.BackAuthentication;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 后台用户管理（系统用户）
 *
 * @author : Zhao Da
 * @since : 2019/4/29 14:42
 */
@RestController
@RequestMapping(value = "/back/user")
@CrossOrigin(origins = "*", allowedHeaders = "Authorization,loginResult,Access-Control-Allow-Origin, Content-Type,accept")
public class BackUserController {

    @Autowired
    private SysAdminService sysAdminService;

    @ApiOperation(value = "（后台）用户管理-查询", notes = "用户管理。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码（1,2,3...）", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页展示数", required = true, dataType = "Int"),
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "角色id", required = false, dataType = "Long"),})
    @GetMapping("search")
    @BackAuthentication(code = "18")
    public PageResponseDto<SysAdmin> search(@RequestParam("pageNum")int pageNum,
                                            @RequestParam("pageSize")int pageSize,
                                            SysAdminDto backUserDto)
    {
        PageInfo<SysAdmin> pageInfo = sysAdminService.selectList(pageNum, pageSize, backUserDto);
        return new PageResponseDto<>(pageInfo);
    }

    @ApiOperation(value = "（后台）用户管理-添加", notes = "添加用户,返回插入数量>0成功。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "userName", value = "用户名", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "用户角色ID", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "remark", value = "备注", required = false, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "realName", value = "真实姓名", required = true, dataType = "string"),
    })
    @RequestMapping(value = "add" ,method = RequestMethod.POST)
    @BackAuthentication(code = "21")
    public ResponseDto<Integer> add(HttpServletRequest request, SysAdminDto backUserDto) {
        ResponseDto<Integer> result = new ResponseDto<>(ResponseCodeEnum.SUCCESS);
        Integer data = sysAdminService.insert(backUserDto);
        result.setData(data);
        return result;
    }

    @ApiOperation(value = "（后台）用户管理-修改", notes = "更新用户信息。")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "roleId", value = "用户角色ID", required = false, dataType = "Long"),
            @ApiImplicitParam(paramType = "query", name = "remark", value = "备注", required = false, dataType = "string"),
    })
    @RequestMapping(value = "update" ,method = RequestMethod.POST)
    @BackAuthentication(code = "22")
    public ResponseDto<Integer> update(HttpServletRequest request, SysAdminDto backUserDto) {
        ResponseDto<Integer> result = new ResponseDto<>(ResponseCodeEnum.SUCCESS);
        String loginNameAes = (String) request.getAttribute("loginName");
        if (StringUtils.isNotBlank(loginNameAes)) {
            backUserDto.setModifier(AESUtil.getDecrypt(loginNameAes, Constant.AES_KEY));
        }
        Integer data = sysAdminService.update(backUserDto);
        result.setData(data);
        return result;
    }
}
